<?php

class Theater extends AppModel {

    var $actsAs = array('Auditable');
    var $name = 'Theater';
    var $displayField = 'name';
    var $validate = array(
        'name' => array(
            'notempty' => array(
                'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'address' => array(
            'notempty' => array(
                'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'start_date' => array(
            'notempty' => array(
                'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'finish_date' => array(
            'notempty' => array(
                'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );
    //The Associations below have been created with all possible keys, those that are not needed can be removed

    var $hasMany = array(
        'TheatersCostume' => array(
            'className' => 'TheatersCostume',
            'foreignKey' => 'theater_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'TheatersEmployee' => array(
            'className' => 'TheatersEmployee',
            'foreignKey' => 'theater_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'TheatersExpense' => array(
            'className' => 'TheatersExpense',
            'foreignKey' => 'theater_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );
    var $hasAndBelongsToMany = array(
        'Costume' => array(
            'className' => 'Costume',
            'joinTable' => 'theaters_costumes',
            'foreignKey' => 'theater_id',
            'associationForeignKey' => 'costume_id',
            'unique' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'deleteQuery' => '',
            'insertQuery' => ''
        ),
        'Employee' => array(
            'className' => 'Employee',
            'joinTable' => 'theaters_employees',
            'foreignKey' => 'theater_id',
            'associationForeignKey' => 'employee_id',
            'unique' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'deleteQuery' => '',
            'insertQuery' => ''
        )
    );

    function payAll($employee_id, $date_from, $date_to, $pagar_caja) {

        //si tiene que pagar con la caja, registramos este movimiento
        if ($pagar_caja) {

            //si el dinero alcanza, vamos a registrar cada uno de estos 
            //movimientos en la caja
            $sql = 'SELECT theater_id, SUM(TheatersEmployee.cost) as total
                FROM theaters_employees as TheatersEmployee
                WHERE paid = 0
                AND TheatersEmployee.employee_id = ' . $employee_id . '
                AND TheatersEmployee.theater_id IN (SELECT Theater.id
                                                    FROM theaters as Theater
                                                    WHERE Theater.start_date >= "' . $date_from . '"
                                                     AND Theater.finish_date <= "' . $date_to . '"
                                                              )
                GROUP BY theater_id';

            $registros = $this->query($sql);

            //me traigo el modelo
            App::import('Model', 'CashMovement');
            $this->CashMovement = new CashMovement();

            foreach ($registros as $reg) {
                $array_mov = array();
                $array_mov['user_id'] = $_SESSION['Auth']['User']['id'];
                $array_mov['type'] = 'out';
                $array_mov['movement'] = 'employee';
                $array_mov['amount'] = $reg[0]['total'];
                $array_mov['theater_id'] = $reg['TheatersEmployee']['theater_id'];
                $array_mov['employee_id'] = $employee_id;

                //guardamos el movimiento
                $this->CashMovement->create();
                $this->CashMovement->save($array_mov);
            }
        }

        //ahora marcamos como pagadas las obras
        $sql = 'UPDATE theaters_employees as TheatersEmployee
                SET paid = 1
                WHERE TheatersEmployee.employee_id = ' . $employee_id . '
                AND TheatersEmployee.theater_id IN (SELECT Theater.id
                                                    FROM theaters as Theater
                                                    WHERE Theater.start_date >= "' . $date_from . '"
                                                     AND Theater.finish_date <= "' . $date_to . '"
                                                              )';

        return $this->query($sql);
    }

}
